//
//  GRXSettingCell.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/11.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXSettingCell.h"
#import "GRXSettingItem.h"
#define arc4random_uniform(r,g,b) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:1]
@interface GRXSettingCell ()

{
    UIImageView *_arrow;//用来记录cell循环利用时候产生的imageView(箭头)
    UISwitch *_switch;//用来记录cell循环利用产生的switch开关
    UILabel *_label;
    
}
@end

@implementation GRXSettingCell
//初始化cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //1.修改cell的文字颜色和大小
        self.textLabel.numberOfLines = 0;
        self.textLabel.textColor = [UIColor blackColor];
        self.textLabel.font = [UIFont systemFontOfSize:14];
        

        
        //2.设置背景色
        UIView *viewBG = [[UIView alloc] init];
        viewBG.backgroundColor = [UIColor whiteColor];
        self.backgroundView = viewBG;
        
        //3.设置被选中的背景色
        UIView *selBG = [[UIView alloc] init];
        selBG.backgroundColor = arc4random_uniform(238,234,223);
        self.selectedBackgroundView = selBG;
       
    }
    return  self;
}

//重写model的set方法
- (void)setItem:(GRXSettingItem *)item{
    _item = item;
    self.textLabel.text = item.title;
    
    //设置cell的选中样式
    self.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    //判断是否有子标题
    if (item.subTitle) {
        self.detailTextLabel.text = item.subTitle;
        self.detailTextLabel.textColor = [UIColor lightGrayColor];
        self.detailTextLabel.font = [UIFont systemFontOfSize:14];
    }
    

    
    //判断模型的类型
    if (item.type == GRXSettingCellTypeArrow) {
        
        [self setUpArrow];
        
    }else if(item.type == GRXSettingCellTypeSwitch)
    {
        
        [self setUpSwitch];
        
        
    }else if(item.type == GRXSettingCellTypeLabel){
        
        [self setUpLabel];
        
    }else{
        //防止cell的循环利用,造成界面混乱
        self.accessoryView = nil;
        
    }

}

/************开始设置cell右边的显示样式**************/
//添加右箭头
- (void)setUpArrow
{
    //判断是否已经存在带箭头的控件
    if (_arrow == nil) {
        _arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];
    }
    self.accessoryView = _arrow;
}


//添加右边的switch按钮
- (void)setUpSwitch
{
    //设置cell的选中不会变色
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //判断是否已经存在switch控件
    if (_switch == nil) {
        _switch = [[UISwitch alloc] init];
        _switch.onTintColor = kGRXColor(255, 70, 112);
        _switch.on = YES;
    }
    //设置右边为seitch开关
    self.accessoryView = _switch;
}

//添加右边的label
- (void)setUpLabel
{
    if (!_label) {
        _label = [[UILabel alloc] init];
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
        
        _label.numberOfLines = 0;
        
        _label.frame = CGRectMake(screenW - 180, 0, 150, self.bounds.size.height);
        [self.contentView addSubview:_label];
        
        _label.textAlignment = NSTextAlignmentRight;
        _label.textColor = [UIColor grayColor];
        _label.font = [UIFont systemFontOfSize:14];
    }
    _label.text = _item.detail;
    [self setUpArrow];
}


@end
